import html from './index.html';
import { getToken, getUser, logout } from 'utils';

const token = getToken();
const user = getUser();
const pageName = location.pathname.split(/[\/.]/)[1];
const $html = $(html);

if (token && user) { // 如果已登录
  $html.find('.logout').click(logout);
  $html.find('.myresumes a').attr('href', `/resumes.html?uid=${user.id}`);
  $html.find('#after-login').show();
} else { // 未登录
  $html.find('.login').attr('href', `/login.html?redirect=${pageName}`);
  $html.find('.register').attr('href', `/register.html?redirect=${pageName}`);
  $html.find('#before-login').show();
}

$.fn.nav = function (opt) {
  let def = {
    active: `.${pageName}`, // 当前页面对应的tab标签的selecter、element或jQuery对象
    position: 'static-top', // 导航条位置：static-top|fixed-top|fixed-bottom
  };
  opt = $.extend({}, def, opt);

  this.addClass(`navbar navbar-inverse navbar-${opt.position}`);
  if (opt.position.startsWith('fixed')) $('body').css({ 'padding-top': this.height() + 1 });
  this.css('margin-bottom', 0);
  $html.find(opt.active).addClass('active');
  this.html($html);
};